﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Google.GData.Spreadsheets;
using Google.GData.Client;

namespace Copycat
{
    public partial class SuggesstionsManagment : System.Web.UI.Page
    {
        public struct sender
        {
            public string name;
            public string tel;
            public string email;
            public string message;
            public string subject;
            public string status;
        }
        private List<sender> senders;
        protected void Page_Load(object sender, EventArgs e)
        {

            SpreadsheetsService service = (SpreadsheetsService)Session["Service"];
            SpreadsheetFeed feed = (SpreadsheetFeed)Session["Feed"];

            if (IsPostBack)
            {
                
                senders = (List<sender>)Session["senders"];
                return;
            }

            if (feed == null)
            {

                service = new SpreadsheetsService("NewsletterService");

                service.Credentials = new GDataCredentials("jobs.copycat@gmail.com", "copycat_2012");

                // Instantiate a SpreadsheetQuery object to retrieve spreadsheets.
                SpreadsheetQuery query = new SpreadsheetQuery();

                // Make a request to the API and get all spreadsheets.
                feed = service.Query(query);

                if (feed.Entries.Count == 0)
                    return;
            }

            //// TODO: Choose a spreadsheet more intelligently based on your
            //// app's needs.

            SpreadsheetEntry spreadsheet = new SpreadsheetEntry();
            foreach (SpreadsheetEntry en in feed.Entries)
                if (en.Title.Text == "complaints")
                    spreadsheet = en;

            WorksheetFeed wsFeed = spreadsheet.Worksheets;
            WorksheetEntry worksheet = (WorksheetEntry)wsFeed.Entries[0];

            // Define the URL to request the list feed of the worksheet.
            AtomLink listFeedLink = worksheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null);

            // Fetch the list feed of the worksheet.
            ListQuery listQuery = new ListQuery(listFeedLink.HRef.ToString());
            ListFeed listFeed = service.Query(listQuery);

            sender s= new sender();
            senders = new List<sender>();
            foreach (ListEntry ae in listFeed.Entries)
            {

               ListBox1.Items.Add(ae.Elements[0].Value);

               s = new sender(); 
                s.name = ae.Elements[0].Value;
               s.email = ae.Elements[1].Value;
               s.tel = ae.Elements[2].Value;
               s.subject = ae.Elements[3].Value;
                s.message = ae.Elements[4].Value;
                if (ae.Elements[5].Value == "0")
                    s.status = "Not Answered";
                else
                    s.status = "Answered";
                senders.Add(s);
            }
            Session["senders"] = senders;
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = ListBox1.SelectedIndex;
            if (index == -1)
                return;

            sender s = senders[index];
            txtMail.Text = s.email;
            txtTel.Text = s.tel;
            txtmsg.Text = s.message;
            txtSubject.Text = s.subject;
            txtStatus.Text = s.status;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            DatabaseManager.DeleteRecord("complaints","tel", txtTel.Text);
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            btnSend.Visible = true;
            lblReply.Visible = true;
            txtReply.Visible = true;

            
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            string body = "<b><u>Original Message</u> :</b>";
            body += "<br/>";
            body += txtmsg.Text;
            body += "<br/><br/>";
            body += "<b><u>Adminstrator</u> :</b>";
            body += "<br/>";
            body += txtReply.Text;
                
            MailSender.SendMail(txtMail.Text, "Re: " + txtSubject.Text, body);

            DatabaseManager.SetComplaintStatus(txtTel.Text, "1");
        }
    }
}
